﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webchat.Models;

namespace Webchat.Controllers
{
    public class UserController : Controller
    {
        private UserRepository userRep;

        public UserController()
        {
            userRep = new UserRepository();
        }
        //
        // GET: /Channel/

        public ActionResult Index()
        {
            List<aspnet_User> users = userRep.showAllUsers().ToList();
            return View(users);
        }

        //
        // GET: /Channel/Details/5

        public ActionResult Details(String id)
        {
            aspnet_User User = userRep.showUser(id);
            if (User == null) 
                return View("NotFound");
            return View(User);
        }

        //
        // GET: /Channel/Create

        public ActionResult Create()
        {
            return View(new aspnet_User());
        } 

        //
        // POST: /Channel/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Channel/Edit/5
 
        public ActionResult Edit(String id)
        {
            aspnet_User User = userRep.showUser(id);
            if (User == null)
                return View("NotFound");
            return View(User);
        }

        //
        // POST: /Channel/Edit/5

        [HttpPost]
        public ActionResult Edit(String id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                aspnet_User User = userRep.showUser(id);
                UpdateModel(User);
                userRep.updateUser(User);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Channel/Delete/5
 
        public ActionResult Delete(String id)
        {
            aspnet_User User = userRep.showUser(id);
            if (User == null)
                return View("NotFound");
            return View(User);
        }

        //
        // POST: /Channel/Delete/5

        [HttpPost]
        public ActionResult Delete(String id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                aspnet_User User = userRep.showUser(id);
                if (User == null)
                    return View("NotFound");
                userRep.deleteUser(User);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
